/**
 * Simple Read
 * 
 * Read data from the serial port and change the color of a rectangle
 * when a switch connected to a Wiring or Arduino board is pressed and released.
 * This example works with the Wiring / Arduino program that follows below.
 */


import processing.serial.*;

Serial myPort;  // Create object from Serial class
int val;      // Data received from the serial port
int R;
int G;
int B;
int rNo = 0;
int reset = 1;
void setup() 
{
  size(200, 200);
  // I know that the first port in the serial list on my mac
  // is always my  FTDI adaptor, so I open Serial.list()[0].
  // On Windows machines, this generally opens COM1.
  // Open whatever port is the one you're using.
  String portName = Serial.list()[0];

  myPort = new Serial(this, portName, 9600);

}


void draw()
{
  if ( myPort.available() > 0) {  // If data is available,
    val = myPort.read();         // read it and store it in val
  }
  background(255);             // Set background to white
  if (val == 0) {              // If the serial value is 0,
    fill(0);
    reset = 1;
  }
  else {      // If the serial value is not 0,
    if (reset == 1) {
      rNo = (int) random(1,10);
      if(rNo < 4) {
        R = 231;  G = 47;  B = 39;
      }
      else if(rNo >= 4 && rNo < 7) {
        R = 19; G = 166; B = 50;
      }
      else if(rNo >= 7) {
        R = 1;  G = 134;  B = 141;
      }
    }
    
    fill(R,G,B);                 // set fill to light gray
    reset = 0;
  }
  rect(50, 50, 100, 100);
    // val1 -> start horizontal
    // val2 -> start verticle
    // val3 -> length horizontal
    // val4 -> length verticle
  
}



